package infra.mongodb.document.mapper;

import domain.model.project.Project;
import infra.mongodb.document.MongoDBProjectDocument;
import infra.mongodb.document.MongoDBProjectDocumentImpl;
import util.mongodb.MongoDBException;
import util.mongodb.mapper.MongoDBDocumentMapper;

/**
 * @author Gregory Boissinot
 */
public class MongoDBProjectDocumentMapper implements MongoDBDocumentMapper<Project, MongoDBProjectDocument> {

    public MongoDBProjectDocument toDocument(Project project) {
        return new MongoDBProjectDocumentImpl(MongoDBProjectDocumentImpl.DEFAULT_VERSION, project);
    }

    public Project fromDocument(MongoDBProjectDocument projectDocument) {
        final String version = projectDocument.getVersion();

        if (MongoDBProjectDocumentImpl.DEFAULT_VERSION.equalsIgnoreCase(version)) {
            return projectDocument.getProject();
        }

        throw new MongoDBException(String.format("Unsupported %s version", version));
    }
}
